package com.keyijie.oauth.service;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.ClientRegistrationException;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
public class AppClientDetailsService implements ClientDetailsService {
    /**
     * Load a client by the client id. This method must not return null.
     *
     * @param clientId The client id.
     * @return The client details (never null).
     * @throws ClientRegistrationException If the client account is locked, expired, disabled, or invalid for any other reason.
     */
    @Override
    public ClientDetails loadClientByClientId(String clientId) throws ClientRegistrationException {
        return new ClientDetails() {
            @Override
            public String getClientId() {
                return "ios";
            }

            @Override
            public Set<String> getResourceIds() {
                Set<String> resourceIds = new HashSet<String>();
                resourceIds.add("API");
                return resourceIds;
            }

            @Override
            public boolean isSecretRequired() {
                return true;
            }

            @Override
            public String getClientSecret() {
                return "123456";
            }

            @Override
            public boolean isScoped() {
                return true;
            }

            @Override
            public Set<String> getScope() {
                Set<String> scopes = new HashSet<String>();
                scopes.add("RW");
                return scopes;
            }

            @Override
            public Set<String> getAuthorizedGrantTypes() {
                Set<String> authGrantTypes = new HashSet<String>();
                authGrantTypes.add("password");
                return authGrantTypes;
            }

            @Override
            public Set<String> getRegisteredRedirectUri() {
                return new HashSet<>();
            }

            @Override
            public Collection<GrantedAuthority> getAuthorities() {
                return Collections.<GrantedAuthority>emptyList();
            }

            @Override
            public Integer getAccessTokenValiditySeconds() {
                return -1;
            }

            @Override
            public Integer getRefreshTokenValiditySeconds() {
                return -1;
            }

            @Override
            public boolean isAutoApprove(String scope) {
                return true;
            }

            @Override
            public Map<String, Object> getAdditionalInformation() {
                return new HashMap<String, Object>();
            }
        };
    }
}
